<?php
namespace app\admin\model;

use app\customer\model\Operator;
use think\Model;

class Supplier extends BaseModel
{
    protected $attributes = [
        'sup_type' => [
            1 => '渠道供应商',
            2 => '个人供应商',
            3 => 'KOL',
            4 => '网站'
        ],
        'is_deal' => [
            1 => '是',
            0 => '否'
        ]
    ];

    public function getSuptypeValueAttr($value, $data)
    {
        if ($data['sup_type']) {
            return isset($this->attributes['sup_type'][$data['sup_type']]) ? $this->attributes['sup_type'][$data['sup_type']] : '';
        }
        return '';
    }

    public function getIsdealValueAttr($value, $data)
    {
        if (isset($data['is_deal'])) {
            return isset($this->attributes['is_deal'][$data['is_deal']]) ? $this->attributes['is_deal'][$data['is_deal']] : '';
        }
        return '';
    }

    public function getIsDealAttr($value, $data)
    {
        return (int)$value;
    }

    /**
     * 获取国家名
     * @return \think\model\relation\HasOne
     */
    public function getCountryValue()
    {
        return $this->hasOne(RegionCountry::class, 'id', 'country')->bind(['country_value' => 'name']);
    }

    public function getOwnerValue()
    {
        return $this->hasOne(Operator::class, 'id', 'owner')->bind(['owner_value' => 'nickname']);
    }

    public function getPartnerValueAttr($value, $data)
    {
        if (!isset($data['partner']) || '' == $data['partner']) {
            return '';
        }
        $partnerIds = explode(',', $data['partner']);
        $partnerIds = array_filter($partnerIds, 'strlen');
        $partners = Operator::whereIn('id', $partnerIds)->field('cname')->select()->toArray();

        return implode(',', array_column($partners, 'cname'));
    }
}

?>
